function getName() {
    var user = JSON.parse(localStorage.getItem("loggedinUser"));
    if (user) {
        console.log("Currently logged in: " + user["name"]);
        document.getElementById("name-goes-here").innerText = user["name"];
    } else {
        console.log("No one is logged in");
    }
}
getName(); //run the function

function addtimeListerner(){
    document.getElementById("addtime").addEventListener("click",function(){
        var user=JSON.parse(localStorage.getItem("loggedinUser"));
        if(user){
            user ["day"]++;
            localStorage.setItem("loggedinUser",JSON.stringify(user));
            document.getElementById("checkin-count-goes-here").innerText=user["day"];
            updateUsersday(user["email"],user["day"]);
        }else{
            console.log("No one is logged in");
        }
    });
}
addtimeListerner();

function updateUsersday(email,dayValue){
    var users = JSON.parse(localStorage.getItem("users"));
    if(users){
        const person =users.find(u=>u.email==email);
        if(person){
            person.day=dayValue;
            addToHistory(person);
        }
        localStorage.setItem("users",JSON.stringify(users));
    }
}
function addToHistory(person){
    const now=new Date();
    const formatted =now.toLocaleString('en-US',{
        weekday:'long',
        year:'numeric',
        month:'long',
        day:'numeric',
        hour:'numeric',
        minute:'numeric',
        second:'numeric'
    });
    if(person.history==null)
        person.history=[];
    person.history.push({date:formatted});
}


function getDay() {
    var who = JSON.parse(localStorage.getItem("loggedinUser"));
    if (who) {
        document.getElementById("checkin-count-goes-here").innerText = who["day"];
    } else {
        console.log("No one is logged in");
    }
}
getDay();

function addPlusMinusListener() {
    document.addEventListener("DOMContentLoaded", function () {
        const minusBtn = document.getElementById('minus-btn');
        const plusBtn = document.getElementById('plus-btn');
        const input = document.getElementById('number-input');
        minusBtn.addEventListener('click', function () {
            const currentValue = Number(input.value) || 0;
            if (currentValue > 0) {
                input.value = currentValue - 1;
            }
        });
        plusBtn.addEventListener('click', function () {
            input.value++;
        });
    });

}
addPlusMinusListener();
function addCupsListener() {
    document.getElementById("addCupsButton").addEventListener("click", function () {
        var number = document.getElementById("number-input").value;
        // find out who is logged in
        var user = JSON.parse(localStorage.getItem("loggedinUser"));
        if (user) {
            user["day"] = Number(user["day"]) + Number(number);
            localStorage.setItem("loggedinUser", JSON.stringify(user));

            //get the users collection, and update the count for THAT user
            updateUsersday(user["email"], user["day"]);
            console.log("day: "+user["day"]);
            // display it
            document.getElementById("checkin-count-goes-here").innerText = user["day"];
        } else {
            console.log("No one is logged in");
        }
    })
}
addCupsListener();